/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.generator.standalone;

/**
 *
 * @author Laurent WOUTERS
 */
public class ClassWriter {
    public static void write(String folder, xowl.generator.ClassGenerator cg) throws java.io.IOException {
        java.io.Writer writer = xowl.utils.Files.getWriter(folder + cg.getName() + ".java");
        writer.append("/* This file has been generated by:\n");
        writer.append("   xOWL Code Generator\n");
        writer.append("*/\n");
        writer.append("package " + cg.getPackage().getFullName() + ";\n");
        writer.append("\n");

        if (!cg.isAbstract())
            writer.append("public class " + cg.getName());
        else
            writer.append("public interface " + cg.getName());
        
        // writes parency
        boolean first = true;
        for (xowl.generator.ClassGenerator parent : cg.getSuperClasses()) {
            if (first) {
                if (!cg.isAbstract()) writer.append(" implements ");
                else writer.append(" extends ");
            } else
                writer.append(", ");
            writer.append(parent.getJavaName());
            first = false;
        }
        writer.append(" {\n");

        if (cg.isAbstract())
            for (xowl.generator.PropertyInterface inter : cg.getPropertyInterfaces())
                PropInterWriter.write(writer, inter);
        else
            writeJava_Concrete(writer, cg);
        
        writer.append("}\n");
        writer.close();
    }
    private static void writeJava_Concrete(java.io.Writer writer, xowl.generator.ClassGenerator cg) throws java.io.IOException {
        // writes all Implementations
        for (xowl.generator.PropertyImplementation implementation : cg.getPropertyImplementations()) {
            PropImplWriter.write(writer, implementation);
            writer.append("\n");
        }
        // writes all static instances
        for (xowl.generator.InstanceGenerator instance : cg.getStaticInstances()) {
            InstanceWriter.write(writer, instance);
            writer.append("\n");
        }

        // writes constructor
        writer.append("    public " + cg.getName() + "() {\n");

        // writes Implementations' constructors
        for (xowl.generator.PropertyImplementation implementation : cg.getPropertyImplementations())
            PropImplWriter.write_constructor(writer, implementation);
        writer.append("    }\n");
        writer.append("    \n");
    }
}
